﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;

namespace NetCoreCMS.Web.Areas.WebApi.Controllers
{
    public class User_Cash_PayController : UserController
    {
        public IShopService _shop { get; set; }
        public IShop_BillsService _shop_bills { get; set; }
        [Authorize]
        [HttpGet]        
        public IActionResult Get(decimal order_amount,int shop_id,int payment_id)//payment_id=1奖金，2重消
        {
            //检查用户是否登录
            Users model = GetUserInfo();
            if (model == null)
            {
                return Ok(new { status = 0, msg = "温馨提示：用户尚未登录或已超时！" });
            }            
            if (order_amount <= 0)
            {
                return Ok(new { status = 0, msg = "温馨提示：请输入正确的充值金额！" });
            }
            if (payment_id == 0)
            {
                return Ok(new { status = 0, msg = "温馨提示：请选择正确的支付方式！" });
            }
            var shop = _shop.GetModel(shop_id);
            if (shop == null)
            {
                return Ok(new { status = 0, msg = "温馨提示：你要支付的商户不存在！" });
            }
            if (shop.is_lock == 1)
            {
                return Ok(new { status = 0, msg = "温馨提示：你要支付的商户已锁定！" });
            }
            if (model.cash_point < order_amount && payment_id == 1 || model.shop_point < order_amount && payment_id == 2)
            {
                return Ok(new { status = 0, msg = "温馨提示：金额不足！" });
            }
            var flag = _shop_bills.Add(model.id, shop_id, order_amount, payment_id);
            if (!flag)
            {
                return Ok(new { status = 0, msg = "温馨提示：金额不足支付失败！" });
            }
            //保存成功后返回订单号
            return Ok(new { status=1,msg= "支付成功" });
        }
    }
}